
import { _decorator, Component, Node, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = TrackSkillCtrl
 * DateTime = Thu Mar 07 2024 14:35:20 GMT+0800 (中国标准时间)
 * Author = heyongtao
 * FileBasename = TrackSkillCtrl.ts
 * FileBasenameNoExtension = TrackSkillCtrl
 * URL = db://assets/scripts/componentCtrl/TrackSkillCtrl.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('TrackSkillCtrl')
export class TrackSkillCtrl extends Component {
    // [1]
    // dummy = '';

    private m_target : Node = null;
    private m_speed : number = 500;

    setTarget(tg : Node)
    {
        this.m_target = tg;
    }

    start () {
        // [3]
    }

    update (deltaTime: number) {
        if(!this.m_target) 
        {
            this.node.destroy();
            return;
        }
        const targetPos = this.m_target.getPosition();
        let curPos = this.node.getPosition();
        const direction = new Vec2(targetPos.x-curPos.x,targetPos.y-curPos.y);
        let normalizedDirection = new Vec2(0,0);
        Vec2.normalize(normalizedDirection,direction);
        const moveDistance = this.m_speed * deltaTime;
        // vec2 mul
        const scaledDirection = new Vec2(moveDistance*normalizedDirection.x,moveDistance*normalizedDirection.y);
        // vec2 add
        curPos.x += scaledDirection.x;
        curPos.y += scaledDirection.y;
        this.node.setPosition(curPos);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
